<?php
class util_pagelink
{
    private $config = array(
        'base_url'      =>  '',
        'filter'        =>  '',
        'extra'         =>  array(),
        'total'         =>  0,
        'offset'        =>  0,
        'id'            =>  'pagelink',
        'css_prefix'    =>  'page-',
        'interval'      =>  20,
        'start'         =>  0,
        'caller'        =>  'pagelink',
        'target'        =>  'pagingAjax',  
        'pre_text'      =>  true,
        'first_last'    =>  true,
        'divider'       =>  '<div class="page-divider">...</div>',
        'per_section'   =>  2,
        'jump'          =>  false,
        'css_name'      =>  array( 'first' => 'first', 'last' => 'last', 'current' => 'current', 'jump' => 'jump', 'pre_text' => 'text' ),
        'alt_css'       =>  array('prev', 'prev2', 'next', 'next2'),
        'connector'     =>  'st'
    );

    public function __construct($config=array())
    {
        $this->config = array_merge($this->config,$config);
        
        
        if(isset($_GET[$this->config['connector']]))
            $this->config['start'] = $_GET[$this->config['connector']];
        else
            $this->config['start'] = $this->config['offset'];
    }
    public function __destruct()
    {
        unset($this->config);
    }
    public function get_last()
    {
        return $this->config['interval']+$this->config['start'];
    }
    public function get_first()
    {
        return $this->config['start'];
    }
    public function build_page_link()
    {
        $output = '';
        $page_total = ceil(($this->config['total']-$this->config['offset'])/$this->config['interval']);
        $page_curr_num = ceil(($this->config['start']-$this->config['offset']+1)/$this->config['interval']);
        if(!$page_total || ($page_curr_num<1)) return $output;
        $page_curr_idx = ($page_curr_num -1)*$this->config['interval'] + $this->config['offset'];
        $this->config['start'] = $page_curr_idx;
        $base_url = strpos($this->config['base_url'],'?')? $this->config['base_url'].'&':$this->config['base_url'].'&';
        $base_url .= ($this->config['filter'])? "filter={$this->config['filter']}&" : '';
        foreach ($this->config['extra'] as $key => $value) { $base_url .= "{$key}={$value}&"; }
        $caller = ($this->config['caller'])? " rev=\"{$this->config['caller']}\"" : '';
        $target= ($this->config['target'])? " rel=\"{$this->config['target']}\"" : '';  
        if(!$this->config['jump']) {
            $page_str= "<a href=\"{$base_url}{$this->config['connector']}={$page_curr_idx}\" class=\"{$this->config['css_prefix']}{$this->config['css_name']['current']}\"{$caller} {$target}>{$page_curr_num}</a>";
        }
        else {
            $page_str = "<div id=\"{$this->config['id']}-jump\" class=\"page-jump\">";
            $page_str .= "<a href=\"{$base_url}{$this->config['connector']}={$page_curr_idx}\" class=\"{$this->config['css_prefix']}{$this->config['css_name']['current']}\"{$caller} {$target}>{$page_curr_num}</a>";
            $page_str .= "<div id=\"{$this->config['id']}-jump-dropdown\" style=\"display:none\">";
            for($j=1;$j<=$page_total;$j++){
                $page_idx = ($j -1)*$this->config['interval'] + $this->config['offset'];
                $page_str .= ($j == $page_curr_num)? "<a class=\"selected\" " : "<a ";
                $page_str .= "href=\"{$base_url}{$this->config['connector']}={$page_idx}\"{$caller}>{$j}</a>";
            }
            $page_str .= "</div></div>";
        }
        $has_first = ($page_curr_num == 1)?true:false;
        $has_last = ($page_curr_num == $page_total)?true:false;
        for($i=1;$i<=$this->config['per_section'];$i++){
            if(!$has_first) {
                $page_prev = $page_curr_num-$i;
                if($page_prev == 1) {
                    $has_first = true;
                    $css_name = $this->config['css_prefix'].$this->config['css_name']['first'];
                }
                else {
                    $css_name = $this->config['css_prefix'].$this->config['alt_css'][$i-1];
                }
                $page_prev_idx = $page_curr_idx - ($i*$this->config['interval']);
                $page_str = "<a href=\"{$base_url}{$this->config['connector']}={$page_prev_idx}\" class=\"{$css_name}\"{$caller} {$target}>{$page_prev}</a>".$page_str;
            }
            if(!$has_last) {
                $page_next = $page_curr_num+$i;
                if($page_next == $page_total) {
                    $has_last = true;
                    $css_name = $this->config['css_prefix'].$this->config['css_name']['last'];
                }
                else {
                    $css_name = $this->config['css_prefix'].$this->config['alt_css'][$i-1+$this->config['per_section']];
                }
                $page_next_idx = $page_curr_idx + ($i*$this->config['interval']);
                $page_str .= "<a href=\"{$base_url}{$this->config['connector']}={$page_next_idx}\" class=\"{$css_name}\"{$caller} {$target}>{$page_next}</a>";
            }
        }
        if($this->config['first_last']){
            if(!$has_first){
                $page_prev--;
                $divider = ($page_prev != 1)? $this->config['divider'] : '';
                $page_str = "<a href=\"{$base_url}{$this->config['connector']}={$this->config['offset']}\" class=\"{$this->config['css_prefix']}{$this->config['css_name']['first']}\"{$caller} {$target}>1</a>{$divider}".$page_str;
            }
            if(!$has_last){
                $page_next++;
                $divider = ($page_next != $page_total)? $this->config['divider'] : '';
                $page_total_idx = ($page_total -1)*$this->config['interval'] + $this->config['offset'];
                $page_str .= "{$divider}<a href=\"{$base_url}{$this->config['connector']}={$page_total_idx}\" class=\"{$this->config['css_prefix']}{$this->config['css_name']['last']}\"{$caller} {$target}>{$page_total}</a>";
            }
        }
        $output = "<div id=\"{$this->config['id']}\">";
        $output .= ($this->config['pre_text'])? "<div class=\"{$this->config['css_prefix']}{$this->config['css_name']['pre_text']}\">Page {$page_curr_num}/{$page_total}</div>" : '';
        $output .= "{$page_str}</div>";
        return $output;
    }
}
?>